// pages/shopCard/shopCard.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    detailInfo: '选择地址',
    shopCardNum: 0,
    shopCardList: [],
    shopCard: [],
    orderType: "1",
    remark: '',
    price: 0,
    priceShow: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initCacheList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  selectAddress(){
    wx.chooseAddress({
      success: res=> {
        console.log(res)
        this.setData({
          address: res,
          detailInfo: res.detailInfo,
          addressInfo: res.userName + ' ' + res.telNumber
        })
      }
    })
  },
  initCacheList(){
    wx.getStorage({
      key:"shopCardList",
      success:res => {
        if(!res.data || res.data.length <=0) {
          return
        }
        var shopCard = []
        var price = 0
        for(var i in res.data){
          shopCard[res.data[i]['row']['id']] = res.data[i]
          price += res.data[i]['num'] * res.data[i]['row']['price'] * 100
          // console.log(price)
        }
        

        this.setData({
          shopCardList: res.data,
          shopCardNum: res.data.length,
          shopCard,
          price,
          priceShow: price
        })
        console.log(res)
      }
    })
    wx.getStorage({
      key: 'pesiong',
      success: res=> {
        this.setData({
          peisong: res.data
        })
      }
    })
    
  },
  onSubmit(){
    if(this.data.orderType == 2 && this.data.detailInfo == '选择地址'){
      wx.showToast({
        title: '请选择配送地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    app.requestUrl('/pay', {shopCardList: this.data.shopCardList, remark: this.data.remark, address: this.data.address, orderType: this.data.orderType}, res=> {
      wx.requestPayment({
        timeStamp: res.json.timeStamp,
        nonceStr: res.json.nonceStr,
        package: res.json.package,
        signType: res.json.signType,
        paySign: res.json.paySign,
        success (res) {
          wx.removeStorage({
            key: 'shopCardList'
          })
          wx.showToast({
            title: '支付成功',
            icon: 'none',
            duration: 2000,
            success:res =>{
              wx.navigateTo({
                url: '/pages/shopCard/shopCard'
              })
            }
          })
        },
        fail (res) {
          wx.showToast({
            title: '请支付订单！',
            icon: 'none',
            duration: 2000
          })

        }
      })
    }, 'POST')
  },
  selectOrderType(e){
    if(e.detail == 2) {
      this.setData({
        priceShow: this.data.price + this.data.peisong.price * 100
      })
    }else{
      this.setData({
        priceShow: this.data.price
      })
    }
    this.setData({
      orderType: e.detail
    })
  },
  onChangeRemark(e){
    this.setData({
      remark: e.detail
    })
  },
  openTel(){
    wx.makePhoneCall({
      phoneNumber: '17730334577'
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})